import 'dart:convert';

import 'package:card_swiper/card_swiper.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_tab_list/custom/custom_slider.dart';
import 'package:flutter_tab_list/date/date.dart';
import 'package:shared_preferences/shared_preferences.dart';
class LightScreenModePage extends StatefulWidget {
  int currentIndex;
  bool isAutoScroll;
  bool iskey;
  List<ImageModel> lightScreenList;
  LightScreenModePage(
      {Key key,
        this.currentIndex,
        this.isAutoScroll,
        this.iskey,this.lightScreenList})
      : super(key: key);

  @override
  _LightScreenModePageState createState() => _LightScreenModePageState();
}

class _LightScreenModePageState extends State<LightScreenModePage> {
  Color color01;
  double progressValue = 180.0;
  double progressValue2 = 50.0;
  bool isPointColor = true;
  bool isSensitivity = true;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  Future<List<ImageModel>> getFromDesk() async {
    var sp = await SharedPreferences.getInstance();
    List<String> jsonStr = sp.getStringList(IMAGEMODEL_LIGHTSCREEN_LIST);
    if (jsonStr != null) {
      return jsonStr.map((e) => ImageModel.fromMap(json.decode(e))).toList();
    }
    print('获取成功');
    return List.empty();
  }
  void _removeStartLiat(ImageModel bean) {
    setState(() {
      if (widget.lightScreenList.remove(bean)) {
        _updateList();
        print('删除成功');
      }
    });
  }
  void _updateList() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setStringList(
        IMAGEMODEL_LIGHTSCREEN_LIST, widget.lightScreenList.map((e) => json.encode(e.toMap())).toList());
    print('更新成功');
  }

  void iskeyChange() {
    setState(() {
      widget.iskey = false;
    });
  }
  @override
  Widget build(BuildContext context) {
    return Container(
      height: MediaQuery.of(context).size.height - 250,
      child: ListView(
        shrinkWrap: true,
        children: [
          Container(
            height: 150,
            child: Swiper(
              itemBuilder: (BuildContext context, int index) {
                return ClipRRect(
                  borderRadius: BorderRadius.circular(8),
                  child: Container(
                    color: Color.fromRGBO(27, 56, 96, 1),
                    child: Stack(
                      alignment: Alignment.center,
                      children: [
                        //裁剪圆形
                        Image.asset(
                          widget.lightScreenList[index].image01,
                          color: widget.lightScreenList[index].color01,
                          width: 200,
                          fit: BoxFit.fill,
                        ),
                        Image.asset(
                          widget.lightScreenList[index].image02,
                          color: widget.lightScreenList[index].color02,
                          width: 200,
                          fit: BoxFit.fill,
                        ),
                      ],
                    ),
                  ),
                );
              },
              key: widget.iskey ? ObjectKey(widget.currentIndex) : null,
              itemCount: widget.lightScreenList.length,
              autoplay: widget.isAutoScroll,
              viewportFraction: 0.7,
              scale: 0.6,
              duration: 1000,
              autoplayDelay: 1500,
              autoplayDisableOnInteraction: true,
              index: widget.currentIndex,
              onIndexChanged: (int index) {
                setState(() {
                  widget.currentIndex = index;
                  print('index${index.toString}');
                  iskeyChange();
                });
              },
            ),
          ),
          SizedBox(
            height: 50,
          ),
          Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Radio(
                value: true,
                onChanged: (value){
                  setState(() {
                    this.isPointColor = value;
                  });
                },
                groupValue:this.isPointColor ,
              ),
              Text("落点颜色",
                style: TextStyle(fontSize: 16, color: Colors.white),),
              SizedBox(width: 20,),
              Radio(
                value:false,
                onChanged: (value){
                  setState(() {
                    this.isPointColor = value;
                  });
                },
                groupValue: this.isPointColor,
              ),
              Text("音柱颜色",
                style: TextStyle(fontSize: 16, color: Colors.white),),

            ],
          ),
          Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Radio(
                value: true,
                onChanged: (value){
                  setState(() {
                    this.isSensitivity = value;
                  });
                },
                groupValue:this.isSensitivity ,
              ),
              Text("灵敏度",
                style: TextStyle(fontSize: 16, color: Colors.white),),
              SizedBox(width: 20,),
              Radio(
                value:false,
                onChanged: (value){
                  setState(() {
                    this.isSensitivity = value;
                  });
                },
                groupValue: this.isSensitivity,
              ),
              Text("亮度",
                style: TextStyle(fontSize: 16, color: Colors.white),),

            ],
          ),
          SizedBox(
            height: 20,
          ),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                '颜色',
                style: TextStyle(fontSize: 12, color: Colors.white),
              ),
              CustomSlider(
                  progressValue: progressValue,
                  onProgressValueChanged: (double value) {
                    setState(() {
                      progressValue = value;
                    });
                  },
                  onColorChanged: (Color value) {
                    setState(() {
                      color01 = value;
                      if (isPointColor) {
                        widget.lightScreenList[widget.currentIndex].color02 = color01;
                        _updateList();
                      } else {
                        widget.lightScreenList[widget.currentIndex].color01 = color01;
                        _updateList();
                      }
                    });
                  }),
              Text(
                '亮度',
                style: TextStyle(fontSize: 12, color: Colors.white),
              ),
              new Container(
                width:MediaQuery.of(context).size.width,
                alignment: Alignment.center,
                child: new Slider(
                  autofocus:true,
                  value: progressValue2,
                  inactiveColor: Colors.black12,
                  label: '${progressValue2}',
                  min: 0.0,
                  max: 100.0,
                  divisions: 1000,
                  activeColor: Colors.blue,
                  onChanged: (double) {
                    setState(() {
                      progressValue2 = double.roundToDouble();
                    });
                  },
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}